home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 9 code / Tracks / cdev.cp next >
Encoding:
Text File  |  1992-11-16  |  54.0 KB  |  1,952 lines  |  [TEXT/MPS ]

  1. #include <Controls.h>
  2. #include <Devices.h>
  3. #include <Dialogs.h>
  4. #include <Events.h>
  5. #include <Errors.h>
  6. #include <Files.h>
  7. #include <Memory.h>
  8. #include <Strings.h>
  9. #include <Types.h>
  10. #include <ToolUtils.h>
  11. #include <Packages.h>
  12. #include <Resources.h>
  13. #include <SysEqu.h>
  14. #include <Quickdraw.h>
  15. #include <Windows.h>
  16. #include <OSEvents.h>
  17. #include <Folders.h>
  18.  
  19. #include "cdev.h"
  20. #include "TracksSaveLoad.h"
  21.  
  22. // Copyright 1990, 1991 Orion Network Systems, Inc. All Rights Reserved.
  23.  
  24.  
  25. pascal long cdevMain(short msg,short item,short nitems,short id,
  26.     EventRecord *event, t_cdev *object, WindowPeek theDialog)
  27. {
  28. long        result;
  29.     if (msg == macDev)
  30.         return true ;        /* Always runnable */
  31.     if (msg == initDev) 
  32.         {    
  33.             object =(t_cdev*) NewPtrClear(sizeof(t_cdev)); // Allocate the class 
  34.             if ( object != nil )
  35.             {
  36.  
  37.                 object->fDp = theDialog;
  38.                 object->fWindRefnum = theDialog->windowKind;
  39.                 object->fRsrcID = id;
  40.                 object->fLastItem = nitems;
  41.             }
  42.         }
  43.     if (object != nil)     //    Test for failure
  44.         {
  45.             object->fEvent = event;
  46.             result = object->Message(msg, item);
  47.             if (result == 0) // Close the cdev now. (other close stuff already done )
  48.             {
  49.                 DisposPtr((Ptr)object);
  50.                 return false ;
  51.             }
  52.         }
  53. return((long) object);
  54. }
  55.  
  56. /**************************************************************
  57.  
  58. Function:  Cdev Message Activate: 
  59.             Activates the scroll bar
  60.  
  61. **************************************************************/
  62.  
  63. void    t_cdev::Activate(void)        /*  "activDev"  */
  64. {
  65.     /* Activate Scroll Bars and any controls...  */
  66.     if (fScrollBar != 0L && fInCDEVMode)
  67.         DEnableControl(fScrollBar, true);        /* We send the actual control handle, not the item # */
  68. }
  69.  
  70.  
  71. /**************************************************************
  72.  
  73. Standard Cut,Copy,Paste etc. routines...  Not used here.
  74.  
  75. **************************************************************/
  76.  
  77. void
  78. t_cdev::CmdKey(short c)
  79. {
  80.     switch (c) {
  81.         case 'z': case 'Z':
  82.             break;
  83.         case 'x': case 'X':
  84.             break;
  85.         case 'c': case 'C':
  86.             break;
  87.         case 'v': case 'V':
  88.             break;
  89.     }
  90. }
  91.  
  92. /**************************************************************
  93.  
  94. Name:        CdevMaskToDriver
  95.  
  96. Type:         Procedure Implementation
  97.  
  98. Function:    Sends the mask we have (fBreakMask or fTraceMask) to the driver
  99.             so that it can selectivly collect data coming in.
  100.             Also send the "BreakOnceThenClear" Boolean to the .TRACE driver
  101. Inputs:        kGetCdevBreakMask or kGetCdevTraceMask 
  102.  
  103. Globals:    
  104.  
  105. Output:    
  106.  
  107. Side Effects:    
  108.  
  109. **************************************************************/
  110. OSErr        t_cdev::CdevMaskToDriver(short csCode)
  111. {
  112. TraceParamBlock     tpb;
  113. OSErr                result;
  114.     tpb.u.mask.BreakOnce=1;        // Gets rid of warning-- (use pragma)
  115.     if (fInCDEVMode)
  116.     {
  117.         if (csCode == kGetCdevBreakMask)
  118.             DoCopyMask(fBreakMask,tpb.u.mask.Mask);
  119.         else
  120.         if (csCode == kGetCdevTraceMask)
  121.             DoCopyMask(fTraceMask,tpb.u.mask.Mask);
  122.         tpb.u.mask.BreakOnce = fBreakOnceThenClear;
  123.         result = Control(fTraceRefNum, csCode, (Ptr) &tpb);
  124.         ReportResult(result,kDriverError);
  125.     } else result = noErr;        //    Dont want to set off any alarms here
  126.     return result;
  127. }
  128.  
  129. // unused
  130. void t_cdev::Error(long code)
  131. {
  132.     fStatus = code;
  133. }
  134.  
  135. /**************************************************************
  136.  
  137. ChangeBank
  138.  
  139. Function:    changes the scroll window (area) to another set of mask names...
  140.             It also sets the fMaskIndex to the new window, so check mark hits will change the
  141.             proper bits... 
  142.             A bank number is the number that represents the list of scrollable mask names
  143.             that is showing in the scroll window-- ie a page number... (0-7 or 0-15)
  144.  
  145.  
  146. Inputs:        New bank number- (changed by the scroll bar...)
  147.  
  148.  
  149. **************************************************************/
  150.  
  151. void    t_cdev::ChangeBank(short    NewBankNumber)
  152. {
  153.     if (NewBankNumber < fMinBankSize) NewBankNumber = fMinBankSize;
  154.     if (NewBankNumber > fMaxBankSize) NewBankNumber = fMaxBankSize;
  155.     
  156.     if (NewBankNumber == fMaskIndex) return;        // Already set right..
  157.     fMaskIndex = NewBankNumber;    
  158.     UpdateFields();        /* Check the right boxes etc.. */
  159. }
  160.  
  161. /**************************************************************
  162.  
  163. Name:        CheckMarkHit
  164. Function:    Yes, it handles checkmark hits...  If the option
  165.     key is down, it calls DebugMarkHit.  A checkmark sets or
  166.     clears the Trace mask and sends the mask to the driver.
  167.  
  168. **************************************************************/
  169.  
  170.  
  171. /* Flip the value of the checkmark that is hit- ie toggle that checkmark and
  172. **    update the mask that the driver sees...
  173. */
  174.  
  175. void    t_cdev::CheckMarkHit(short    itemHit)
  176. {
  177. short        BitPos;
  178. Boolean        BitIsSet;
  179. shex        WhichBitOf128;        // Although it says 128 here, it should work with different
  180.                                 // sized masks...
  181.  
  182.  
  183.     /* fMaskIndex is which page of names we are in... depending on fInCDEVMode */
  184.  
  185.     BitPos = (itemHit - fChecks_Start);    
  186.     
  187.     WhichBitOf128 = BitPos + (fMaskIndex * fFieldsPerBank);    /* Produces a number between 0-127 */
  188.     if (WhichBitOf128 > kBitsPerMask) 
  189.     {
  190.         // DebugStr("\pCheckHitErr");    // This hasnt happened yet, I don't expect it to.
  191.         return;
  192.     }
  193.     if (fEvent->modifiers & optionKey)
  194.         {
  195.             DebugMarkHit(BitPos);
  196.         }
  197.     else
  198.         {
  199.             BitIsSet = BitTst((Ptr)fTraceMask,(long)WhichBitOf128);    
  200.             DSetControl(itemHit,!BitIsSet);    /* Reverse it.. */
  201.             if (BitIsSet)
  202.                 BitClr((Ptr)fTraceMask,(long)WhichBitOf128);
  203.             else
  204.                 BitSet((Ptr)fTraceMask,(long)WhichBitOf128);
  205.             CdevMaskToDriver(kGetCdevTraceMask);        /* Send the new Mask to the Driver... */
  206.         }
  207. }
  208.  
  209. /**************************************************************
  210.  
  211. Function:  Calls .TRACE Driver with instruction to clear trace buffer.
  212.  
  213. **************************************************************/
  214.  
  215. void     t_cdev::ClearBufferNow()
  216. {
  217. register    short    result;
  218.  
  219.     result = Control(fTraceRefNum, kClearTraceBuffer,nil);
  220.     DSetLong(iBufferSizeStatic,fTraceStatus.bufferSize);    /* Show buffer size:    */
  221.     ReportResult(result,kDriverError);
  222. }
  223.  
  224. /**************************************************************
  225.  
  226. Called when cdev is closed by user...  deallocates memory.
  227.  
  228. **************************************************************/
  229.  
  230. void    t_cdev::Close()
  231. {
  232.     DoSave(0);        // save user prefs
  233.  
  234.     if (fFieldNames != nil) 
  235.         DisposHandle(fFieldNames);    // dispose the STR# type list
  236.     fFieldNames = nil;
  237.     ReleaseResource((Handle)fBugPicture);    
  238.     // (this) is disposed of in cdevMain.cp
  239.  
  240. }
  241.  
  242. /**************************************************************
  243.  
  244. Copies a mask...  All 128 bits...
  245.  
  246. **************************************************************/
  247.  
  248. void t_cdev::DoCopyMask(MaskType    src,    MaskType dst)
  249. {
  250.     BlockMove((Ptr)src,(Ptr)dst,sizeof(MaskType));
  251. }
  252.  
  253. /**************************************************************
  254.  
  255.     Cdev Message-- Handle Deactivate message.  Deactivate scroll bar 
  256.     so it turns white.
  257.  
  258. **************************************************************/
  259. void    t_cdev::Deactivate(void)    /*  "deactiveDev"  */
  260. {
  261.     if (fScrollBar != 0L && fInCDEVMode)
  262.         /* Deactivate Scroll Bars  */
  263.         DEnableControl(fScrollBar, false);    /* Send the ControlHandle to be deactivated */
  264. }
  265.  
  266. /**************************************************************
  267.  
  268.     Flip the value of the checkmark that is hit- ie toggle that checkmark and
  269.     update the mask that the driver sees..  The sister routine
  270.     of this is CheckMarkHit.
  271.  
  272. **************************************************************/
  273.  
  274. void    t_cdev::DebugMarkHit(short    BitPos)
  275. {
  276. Boolean        BitIsSet;
  277. shex        WhichBitOf128;        // Will work if mask size changes... 
  278. register snum    result;
  279.  
  280.     /* fMaskIndex is which element in this array we are in... */
  281.                 
  282.     WhichBitOf128 = BitPos + (fMaskIndex * fFieldsPerBank);    /* Produces a number between 0-127 */
  283.  
  284.     
  285.     BitIsSet = BitTst((Ptr)fBreakMask,(long)WhichBitOf128);    
  286.     if (BitIsSet)
  287.         BitClr((Ptr)fBreakMask,(long)WhichBitOf128);    
  288.     else
  289.         BitSet((Ptr)fBreakMask,(long)WhichBitOf128);        
  290.         
  291.     DrawABug(BitPos,!BitIsSet);    /* Draw it or erase it, which ever it is not */
  292.  
  293.     result = CdevMaskToDriver(kGetCdevBreakMask);        
  294.     ReportResult(result,kDriverError);
  295. }
  296.  
  297.  
  298. /**************************************************************
  299.     
  300.     DEnableControl enables or disables a control....
  301.  
  302. **************************************************************/
  303. void    t_cdev::DEnableControl(ControlHandle Control,Boolean Enable)
  304. {
  305.  
  306.     /* Enable/Disable scroll bar... This would work for normal buttons too */
  307.     if (Enable)
  308.         HiliteControl(Control,0);        /* Enable item */
  309.     else
  310.         HiliteControl(Control,255);    /* Disable */
  311. }
  312.  
  313. /**************************************************************
  314.  
  315. Gets an item and enables it.  Correctly handles cdev DITL's.
  316.  
  317. **************************************************************/
  318.  
  319. void    t_cdev::DEnableItem(short theitem,Boolean Enable)
  320. {
  321. Handle            Control;
  322. Rect            arect;
  323. short            itemtype;
  324.     GetDItem((GrafPort *)fDp, theitem + fLastItem, &itemtype, &Control, &arect);
  325.     DEnableControl((ControlHandle)Control,Enable);
  326. }
  327.  
  328. /**************************************************************
  329.  
  330. Gets the frame of a DITL Item..
  331.  
  332. **************************************************************/
  333.  
  334. void    t_cdev::GetDItemFrame(short    item,Rect    *r)
  335. {
  336. short    ItemType;
  337. Handle    ItemHandle;
  338.     GetDItem((GrafPort *)fDp, fLastItem + item  , &ItemType, &ItemHandle, r);
  339. }
  340.  
  341. /**************************************************************
  342.  
  343. Loads a preference file, with the name of 'STR ' -4063, which
  344. is currently 'Tracks prefs'.
  345.  
  346. Reads the name of the driver file that user wants to trace.
  347. That file is opened, and two resources are extracted.  One, the
  348. name of the driver (usually preceded by a .), and a list of the
  349. names of the trace points.
  350.  
  351. **************************************************************/
  352.  
  353. void    t_cdev::DoLoad()
  354. {
  355. saveTypeHandle    hSave;
  356. register    short    PrefsResRefNum;
  357. register    short    err;
  358. StringHandle    PStrHand;
  359. Boolean    wasError;
  360. short prefsFolderVol;
  361.  
  362.     wasError = false;    // unless otherwise set...
  363.     //  Get Pref File Name
  364.  
  365.     PStrHand = GetString(-4063);        /* Name of Preference file */
  366.     err = ResError();
  367.     if (PStrHand == nil)         // There was no preference file...  Reset the trace...
  368.         wasError = true;
  369.     else
  370.     {
  371.     //  Open Tracks Prefs file
  372.         HLock((Handle)PStrHand);
  373.         prefsFolderVol = GetFolderVol(kPreferencesFolderType);
  374.         
  375.         PrefsResRefNum = OpenRFPerm((const Str255)*PStrHand, prefsFolderVol, fsRdWrPerm);
  376.         
  377.         HUnlock((Handle)PStrHand);
  378.         err = ResError();
  379.         
  380.         if ((err == noErr) && (PrefsResRefNum != -1))
  381.         {
  382.             // Get fTargetAppName -- eg : "TestDrvr" or whatever your drivers _file_ name is...
  383.             if (fTargetAppName != nil)
  384.                 DisposHandle(fTargetAppName);
  385.                 
  386.             fTargetAppName = GetResource('kDRp', 128);    
  387.             err = ResError();
  388.     
  389.             if (fTargetAppName != nil) 
  390.             {
  391.                 DetachResource(fTargetAppName);
  392.             } else wasError = true;
  393.             
  394.             // Get Saved preferences
  395.         
  396.             hSave = (saveTypeHandle)GetResource(kPrefResType, kPrefResID);
  397.             err = ResError();
  398.  
  399.             if (hSave != nil)    // and there exists a preference file... 
  400.                 {
  401.                     if ((**hSave).Version != kVersionNumber) 
  402.                         {
  403.                         // an old version of prefs is present...
  404.                             ReportResult(afpBadVersNum,kFileError);
  405.                             CloseResFile(PrefsResRefNum);
  406.                             wasError = true;
  407.                         }
  408.                     else
  409.                     {    
  410.                         // Copy the saved preferences into the proper globals...
  411.                         
  412.                         DoCopyMask((**hSave).fTraceMask , fTraceMask);    // move saved prefs to masks
  413.                         DoCopyMask((**hSave).fBreakMask , fBreakMask);
  414.                         fBreakOnceThenClear = (**hSave).fBreakOnceThenClear;
  415.                         fTraceOnStartup = (**hSave).fTraceOnStartup;
  416.                         fBufferSize = (**hSave).fBufferSize = fBufferSize;
  417.                         fAutoWriteOn = (**hSave).fAutoWriteOn;
  418.                         fCurrentSizeIndex = (**hSave).fCurrentSizeIndex;
  419.                         fTraceEnabled = (**hSave).fTraceEnabled;                        
  420.                         ReleaseResource((Handle)hSave);
  421.                     }
  422.                 } else wasError = true;
  423.                 
  424.             CloseResFile(PrefsResRefNum);
  425.     
  426.         } else 
  427.         {
  428.             fTargetAppName = nil;    // Make a default table...
  429.             wasError = true;
  430.         }
  431.         
  432.         if (wasError == false)     
  433.             GetTargAppStuff();        /* Knowing the name of the driver to be traced, get information from that file... */
  434.  
  435.         ReleaseResource((Handle)PStrHand);
  436.         (void) ResError();
  437.     }
  438.     
  439.     // Try to open the TargetApplication
  440.     if (wasError)
  441.     {
  442.     //    Reset the trace....
  443.         
  444.         (void) Control(fTraceRefNum, kSetTraceOffline, nil);
  445.         fTraceEnabled = false;
  446.         
  447.         fTraceOnStartup = false;    // turn off trace from startup
  448.         fBreakOnceThenClear = true;    // will get sent to drvr by FillMask
  449.         
  450.         (void) Control(fTraceRefNum,kStartedFromInit,(Ptr)fTraceOnStartup);
  451.         
  452.         // FillMask(kBreakMaskID, 0);    // Clear Break Mask
  453.         // FillMask(kTraceMaskID, 0);    // and trace mask
  454.         
  455.         Control(fTraceRefNum, kSetAutoWriteOff, nil);
  456.         fAutoWriteOn = false;
  457.         
  458.         if (fDriverDotName != nil) 
  459.             DisposHandle(fDriverDotName);
  460.         fDriverDotName = GetResource('STR ', -4062);    /* <No driver> */
  461.         DetachResource(fDriverDotName);    
  462.         LoadFieldNames();    // This will create a field name list on the fly...
  463.         // ReportResult(resNotFound,kFileError);        
  464.     }
  465.     
  466. }
  467.  
  468. /**************************************************************
  469.  
  470.  given a folder type like 'pref', this will return the vol ref num...
  471.  
  472. **************************************************************/
  473. short t_cdev :: GetFolderVol(OSType type)
  474. {
  475. WDPBRec        wdParamBlock;        /*param block to set up working directory*/
  476. short         result;
  477. short         prefVRef;
  478. long         prefDirID;
  479.  
  480.     result=FindFolder(kOnSystemDisk, type, true, &prefVRef, &prefDirID);
  481.     if (result == 0)
  482.     {
  483.         wdParamBlock.ioCompletion = NULL;
  484.         wdParamBlock.ioNamePtr = NULL;
  485.         wdParamBlock.ioVRefNum = prefVRef;
  486.         wdParamBlock.ioWDDirID = prefDirID;
  487.         result=PBOpenWD(&wdParamBlock, false);
  488.         if (result == 0)
  489.             return wdParamBlock.ioVRefNum;
  490.     }
  491.     return 0;
  492. }
  493.  
  494. /**************************************************************
  495.  
  496. GetTargAppStuff gets the name of the target driver and the str#
  497. list from the target drivers file...
  498.  
  499. **************************************************************/
  500. void t_cdev :: GetTargAppStuff()
  501. {
  502. short     AppsResRefNum, err;
  503. Boolean notVaildName = false;
  504. short vRef;
  505.     
  506.     if (fTargetAppName == nil) {goto fileNotFound;}
  507.     
  508.     HLock(fTargetAppName);
  509.     
  510.     vRef = GetFolderVol(kSystemFolderType);            /* Look in System Folder first */
  511.     if (vRef == 0) vRef = fEnvironment.sysVRefNum;
  512.     
  513.     AppsResRefNum = OpenRFPerm((const Str255)*fTargetAppName, vRef, fsRdWrPerm);
  514.     
  515.         
  516.     if (AppsResRefNum == -1)
  517.     {
  518.     
  519.         if (fEnvironment.systemVersion <= 0x607) goto fileNotFound;
  520.         
  521.         vRef = GetFolderVol(kExtensionFolderType);    /* Look in Extensions Folder */
  522.         AppsResRefNum = OpenRFPerm((const Str255)*fTargetAppName, vRef, fsRdWrPerm);
  523.     }
  524.     
  525.     if (AppsResRefNum == -1)
  526.     {
  527.         vRef = GetFolderVol(kExtensionFolderType);
  528.         AppsResRefNum = OpenRFPerm((const Str255)*fTargetAppName, vRef, fsRdWrPerm);
  529.     }
  530.     
  531.     if (AppsResRefNum == -1)
  532.     {
  533.         vRef = GetFolderVol(kControlPanelFolderType);
  534.         AppsResRefNum = OpenRFPerm((const Str255)*fTargetAppName, vRef, fsRdWrPerm);
  535.     }
  536.     
  537.     if (AppsResRefNum == -1)
  538.     {
  539.         goto fileNotFound;
  540.     }
  541.     
  542.     err = ResError();
  543.     if (err != noErr) goto failed;
  544.     
  545.     HUnlock(fTargetAppName);
  546.  
  547.     // Get DotDriverName -- eg : ".ASNA"
  548.     if (fDriverDotName != nil)
  549.         DisposHandle(fDriverDotName);
  550.  
  551.     fDriverDotName = GetResource('DrvN', 128);
  552.     err = ResError();
  553.     
  554.     if (fDriverDotName == nil) 
  555.     {
  556.         fDriverDotName = GetResource('STR ', -4062);    // currently = <No Driver>
  557.         /* Contains no trace information */
  558.         notVaildName = true;
  559.     }
  560.     DetachResource(fDriverDotName);    
  561.     
  562.     LoadFieldNames();        // Will build default table if STR# 777 not there
  563.     CloseResFile(AppsResRefNum);
  564.     err = ResError();
  565.     
  566.     if (notVaildName) goto failed;
  567.     
  568.     ReportResult(0, kDriverError);        // report noError
  569.     return;
  570.     
  571. fileNotFound:
  572.     ReportResult(kInternalMsg, 10);        // fileNotFound
  573.     return;
  574.     
  575. failed:
  576.     ReportResult(kInternalMsg, 10);        // Driver contains no trace info.
  577.     return;
  578.     
  579. }
  580. /**************************************************************
  581. // Do save saves the following:
  582. //    fTargetAppName:  eg Snaps Access..  Whichever driver user is tracing...
  583. //    hSave:  Holds other user info
  584. **************************************************************/
  585. void    t_cdev::DoSave(Boolean JustErasePrefs)
  586. {
  587. Handle    h;
  588. register saveTypeHandle    hSave;
  589. register    short    PrefsResRefNum;
  590. register    short    err;
  591. saveType    Prefs;
  592. Handle        PStrHand;
  593. short         prefsFolderVol;
  594. FInfo        finfo;
  595.  
  596.     PStrHand = GetResource('STR ', -4063);        // Name of Preference file 
  597.     if (PStrHand == nil) 
  598.     {
  599.         ReportResult(resNotFound,kFileError);
  600.         return;
  601.     }
  602.     prefsFolderVol = GetFolderVol(kPreferencesFolderType);
  603.     PrefsResRefNum = OpenRFPerm((const Str255)*PStrHand, prefsFolderVol, fsRdWrPerm);
  604.  
  605.     err = ResError();
  606.     if ((err !=noErr) || (PrefsResRefNum == -1))
  607.         {
  608.             if ((err != eofErr) && (err != fnfErr))    // -39 or -43
  609.                 {
  610.                     ReportResult(err,kFileError);                
  611.                     return;        // We handle only empty resource errors...
  612.                 }
  613.  
  614.             SetVol(nil, prefsFolderVol);
  615.             CreateResFile((const Str255)*PStrHand);
  616.             
  617.             
  618.             err = ResError();
  619.             if (err == noErr)
  620.                 PrefsResRefNum = OpenRFPerm((const Str255)*PStrHand, prefsFolderVol, fsRdWrPerm);
  621.             err = ResError();
  622.             if (err == noErr)
  623.             {
  624.                 GetFInfo((const Str255)*PStrHand, prefsFolderVol, &finfo);
  625.                 finfo.fdType = kPrefsOSType;
  626.                 finfo.fdCreator = kCDEVCreator;
  627.                 SetFInfo((const Str255)*PStrHand, prefsFolderVol, &finfo);
  628.             }
  629.         }
  630.     else
  631.     if (err == noErr)
  632.         {    // There are resources present... 
  633.             h = GetResource(kPrefResType, kPrefResID);
  634.             if (h != nil)    // and there exists a preference file... 
  635.                 {
  636.                     RmveResource(h);
  637.                     DisposHandle(h);    // and ditch the memory
  638.                 }
  639.                 
  640.             h = GetResource('kDRp', 128);        // The Targ App's Name
  641.             if (h != nil)    // and there exists a preference file... 
  642.                 {
  643.                     RmveResource(h);
  644.                     DisposHandle(h);    // and ditch the memory
  645.                 }
  646.             
  647.             UpdateResFile(PrefsResRefNum);
  648.         }
  649.         
  650.     // ready to create and add our own resoruce... 
  651.     err = ResError();        // Clear ResError so cdev won't bail
  652.     
  653.     hSave = (saveTypeHandle)NewHandleClear((long)sizeof(saveType));
  654.     if (hSave == nil)     
  655.         {
  656.              ReportResult((short) mFulErr, (short) kFileError); // Unlikly scenario
  657.              CloseResFile(PrefsResRefNum);
  658.              return;
  659.         }
  660.     if (JustErasePrefs == false)    
  661.     {    
  662.         UpdateTraceStatusBlk();
  663.         
  664.         Prefs.Version = kVersionNumber;
  665.         DoCopyMask(fTraceMask, Prefs.fTraceMask);
  666.         DoCopyMask(fBreakMask, Prefs.fBreakMask);
  667.         Prefs.fBreakOnceThenClear = (fBreakOnceThenClear) ? 1:0;
  668.         Prefs.fTraceOnStartup = (fTraceOnStartup) ? 1:0;
  669.         Prefs.fBufferSize = fTraceStatus.bufferSize;
  670.         Prefs.fCurrentSizeIndex = fTraceStatus.bufferSizeIndex;
  671.  
  672.         Prefs.fAutoWriteOn = fAutoWriteOn;
  673.         Prefs.fTraceEnabled = fTraceEnabled;
  674.         Prefs.fUnused1 = nil;
  675.         Prefs.fUnused2 = nil;
  676.         Prefs.fUnused3 = nil;
  677.         Prefs.fUnused4 = nil;
  678.         HLock((Handle)hSave);
  679.         BlockMove((Ptr)&Prefs,(Ptr)*hSave,sizeof(saveType));
  680.         HUnlock((Handle)hSave);
  681.     } // else- empty hSave handle will be saved-- all zeros...
  682.     
  683.     AddResource((Handle)hSave, kPrefResType, kPrefResID, "\pStartup Prefs");
  684.     WriteResource((Handle)hSave);
  685.     
  686.     if (fTargetAppName != nil)
  687.         {
  688.             AddResource(fTargetAppName, 'kDRp', 128, "\pTarget App");
  689.             WriteResource(fTargetAppName);
  690.         }
  691.         
  692.     CloseResFile(PrefsResRefNum);        // Updates & closes
  693.     FlushVol(nil, prefsFolderVol);
  694.     ReleaseResource(PStrHand);
  695. }
  696. /**************************************************************
  697. DrawABug
  698. Draws a bug (breakpoint mark) next to the mask name, to the left of the scroll bar.
  699. **************************************************************/
  700. void    t_cdev::DrawABug(short bugIndex,Boolean    Draw)
  701. {
  702. register Rect        DrawBugArea,wh,*picRect;
  703.     SetPort((GrafPtr)fDp);
  704.     if ((bugIndex > fFieldsPerBank) || (bugIndex <0)) return;    // just in case...
  705.     
  706.     GetDItemFrame(fDebugChecks_Start + bugIndex,&DrawBugArea);
  707.  
  708.     if (Draw)
  709.     {
  710.         HLock((Handle)fBugPicture);
  711.         picRect = &(**fBugPicture).picFrame;
  712.         wh=DrawBugArea;
  713.         OffsetRect(&wh,2,2);
  714.         wh.right = wh.left + (picRect->right - picRect->left);
  715.         wh.bottom = wh.top + (picRect->bottom - picRect->top);
  716.         DrawPicture(fBugPicture,&wh);
  717.         HUnlock((Handle)fBugPicture);
  718.     }
  719.     else
  720.     EraseRect(&DrawBugArea);
  721. }
  722.  
  723.  
  724. /**************************************************************/
  725. // Get the Driver's version of the mask, and put it in our (the cdev's) copy...
  726. // If whichMask == 1, get the Breakpoint mask.
  727. // otherwise, get the trace one...
  728. /**************************************************************/
  729. OSErr        t_cdev::DriverToCdevMask(short    csCode)
  730. {
  731. TraceParamBlock     tpb;
  732. OSErr                result = 0;
  733.     if ((csCode == kSendCdevBreakMask) || (csCode == kSendCdevTraceMask))
  734.     {
  735.         result = Status(fTraceRefNum, csCode, (Ptr) &tpb);
  736.         if (result == 0)
  737.         {
  738.             if (csCode == kSendCdevBreakMask)
  739.                 DoCopyMask(tpb.u.mask.Mask,fBreakMask);
  740.             else
  741.             if (csCode == kSendCdevTraceMask)
  742.                 DoCopyMask(tpb.u.mask.Mask,fTraceMask);
  743.             fBreakOnceThenClear = tpb.u.mask.BreakOnce;    // Breakpoint method
  744.         }
  745.         ReportResult(result,kDriverError);
  746.     }    // won't happen..
  747.     return result;
  748. }
  749.  
  750. /**************************************************************
  751. Changes a control (only if it needs to be changed)
  752. **************************************************************/
  753. void    t_cdev::DSetControl(short theitem,short value)
  754. {
  755. ControlHandle    Control;
  756. Rect                arect;
  757. short                itemtype;
  758. short OldValue;
  759.     GetDItem((GrafPort *)fDp, theitem + fLastItem, &itemtype, (Handle *)&Control, &arect);
  760.     SetPort((GrafPort *)fDp);
  761.     if (Control != nil) // just in case
  762.         {
  763.         // Only draw to the screen if it really needs to be drawn
  764.         OldValue = GetCtlValue(Control);
  765.         if (OldValue != value)
  766.             SetCtlValue(Control,value);
  767.         }
  768. }
  769. /**************************************************************
  770.  
  771. Allocates/Deallocates System Heap Space for Trace Buffer... 
  772.  
  773. **************************************************************/
  774.  
  775. OSErr    t_cdev::EnableBuffer(Boolean    On)
  776. {
  777. short        result;
  778. TraceParamBlock     param;
  779.  
  780.     if (On)
  781.         {
  782.  
  783.         result = Control(fTraceRefNum, kDisableTraceBuffer, nil);
  784.         
  785.         param.u.enable.traceBuffSize = fBufSizesHandle[fCurrentSizeIndex];
  786.         param.u.enable.traceBuffSizeIndex = fCurrentSizeIndex;            
  787.         result = Control(fTraceRefNum, kEnableTraceBuffer, (Ptr) ¶m);
  788.         if (result != 0)
  789.             {
  790.                 return result;
  791.                 /*    fCurrentSizeIndex =0; Zero Bytes...     */
  792.                 /*     EnableTrace(false);     Disable trace, since buffer size is zero.. */
  793.             }
  794.         FlushEvents(everyEvent,0);
  795.         }
  796.     else
  797.         {
  798.             result = Control(fTraceRefNum, kDisableTraceBuffer, (Ptr) ¶m);
  799.         }
  800.     
  801.     
  802.     result = UpdateTraceStatusBlk();
  803.     if (result == 0)
  804.         DSetLong(iBufferSizeStatic,fTraceStatus.bufferSize);    /* Show buffer size:    */
  805.     fBufferSize = fTraceStatus.bufferSize;
  806.     fBytesBuffered = 0L;        
  807.     ReportResult(result,kDriverError);        // from last updatetrace...
  808.     return (result);
  809. }
  810.  
  811. /**************************************************************
  812.  
  813. Given a filename and vrefnum, creates this file for trace output
  814.  
  815. // Changes the file that the trace driver writes to.  This is currently not
  816. // changable by the user...  Output is always to the default, which is set once
  817. // at driver startup time....
  818.  
  819. **************************************************************/
  820. OSErr    t_cdev::EnableFile(char     *fName, short    vRefNum)
  821. {
  822. short                    result;
  823. TraceParamBlock         param;
  824. char                    fileStr[255];    
  825.     param.u.enableFile.fileName = fName;
  826.     param.u.enableFile.vRefNum = vRefNum;
  827.     
  828.     result = Control(fTraceRefNum, kSetTraceFileName, (Ptr) ¶m);
  829.     
  830.     if (result == 0)
  831.         {
  832.             (void) UpdateTraceStatusBlk();
  833.             fBytesWritten = 0L;    /* ? */
  834.         }
  835.     ReportResult(result, kDriverError);
  836.     return result;
  837. }
  838.  
  839. /**************************************************************
  840.  
  841. Enables/Disables Trace driver...
  842.  
  843. **************************************************************/
  844. OSErr        t_cdev::EnableTrace(Boolean    On)
  845. {
  846. short    result;
  847.     if (On)
  848.     {    
  849.         if (fDriverDotName != nil)
  850.             result = Control(fTraceRefNum, kSetTraceOnline, (Ptr) *fDriverDotName);
  851.         else result = dInstErr;    // driver install error
  852.     }
  853.     else
  854.         result = Control(fTraceRefNum, kSetTraceOffline, nil);
  855.     if (result == 0)
  856.         SetOnOff(On);
  857.     ReportResult(result,kDriverError);
  858.     return result;
  859. }
  860.  
  861. /**************************************************************
  862.  
  863. // This is the extended dialog portion of the cdev...  It is a modal dialog that
  864. // handles events in a similar way that the cdev does- although instead of using ItemHit()
  865. // it handles its handles button presses, etc inline.  
  866. // The setup required in the beginning saves all cdev varibles that it will modify
  867. // and then modifies them to conform to the new dialog.  This way drawing routines
  868. // can be used by both the cdev's event handler and this modal dialogs...
  869. // At the end, the trace is turned back on and the breakpoints/tracepoints are set
  870. // according to their new values (if any) or if cancel is chosen, the old values
  871. // are restored.
  872. //
  873. // Extended dialog constants use the prefix ke instead of just k.
  874.  
  875. **************************************************************/
  876.  
  877. Boolean t_cdev::ExtendedSetup(Ptr saveGlobsPtr)
  878. {
  879. Rect    extScrollBarRect;
  880. register SavedGlobalsPtr saveGlobs;
  881.     saveGlobs = (SavedGlobalsPtr)saveGlobsPtr;
  882.     Deactivate();        // Deactivate the cdev before we change fScrollBar
  883.     
  884.     saveGlobs->ScrollBarControl = fScrollBar;
  885.     saveGlobs->MaskIndex =    fMaskIndex;
  886.     saveGlobs->DP = fDp;
  887.     saveGlobs->LastItem = fLastItem;
  888.     saveGlobs->InitialTraceState = fTraceEnabled;        // On Off State of trace
  889.     saveGlobs->BOTCCopy = fBreakOnceThenClear;
  890.     saveGlobs->TraceStartup = fTraceOnStartup;
  891.     saveGlobs->AutoWriteOn = fAutoWriteOn;
  892.     if (fTraceEnabled)
  893.         {
  894.             EnableTrace(false);
  895.         }
  896.     DoCopyMask(fTraceMask,saveGlobs->TraceMaskCopy);        // Save a copy of the masks
  897.     DoCopyMask(fBreakMask,saveGlobs->BreakMaskCopy);        // so we can revert.
  898.  
  899.     fDp = (WindowPeek)GetNewDialog(kExtendedDialogID,nil,(WindowPtr)-1);
  900.     if (fDp == nil) return false;
  901.     SetPort((GrafPtr)fDp);
  902.     
  903.     fLastItem = 0;// We no longer have an unknown # of extra D Items (unlike a cdev)
  904.     
  905.     fMinBankSize = keMinBankSize;
  906.     fMaxBankSize = keMaxBankSize;
  907.     fFieldsPerBank = keFieldsPerBank;
  908.     fInCDEVMode = false;
  909.     
  910.     // Position new scroll window 
  911.     // fMaskIndex = (saveGlobs->MaskIndex * kFieldsPerBank) % keFieldsPerBank; 
  912.     fMaskIndex = 1;    // I couldnt get the above line to link. hmph!
  913.     
  914.     fChecks_Start = keChecksStart;
  915.     fChecks_End = keChecksEnd;
  916.     fNames_Start = keNamesStart;
  917.     fNames_End = keNamesEnd;
  918.     fDebugChecks_Start = keDebugAreaStart;
  919.     fDebugChecks_End = keDebugAreaEnd;
  920.     fMaskNumStatic = keMaskNumStatic;
  921.     fErrorMsgStatic = keErrorMsgStatic;
  922.     fErrorNumberStatic = keErrorNumberStatic;
  923.     
  924.     
  925.     /* Get and position Scroll Bar */
  926.     /* Get the size and location of scroll bar (from a user item template) */
  927.     
  928.     GetDItemFrame(keScrollBarArea,&extScrollBarRect);        /* Get area of scroll bar (a user item ditl) */
  929.     fScrollBar = NewControl((GrafPort *) fDp,&extScrollBarRect,"\pBigSBar",true,fMaskIndex,
  930.         keMinBankSize, keMaxBankSize,scrollBarProc,0L);        /* And create a scroll bar of that size */
  931.     if (fScrollBar == nil) Debugger();    // What can we do?  Let it crash...
  932.     
  933.     UpdateFields();
  934.     // Draw anything not handled in the cdev's UpdateFields...
  935.  
  936.     DSetControl(keClearOnceCheckBox,!fBreakOnceThenClear);
  937.     DSetControl (keTraceOnStartup, fTraceOnStartup);
  938.     DSetControl(keAutoWrite,fAutoWriteOn);
  939.     
  940.     MoveTo((*fDp).port.portRect.left,extScrollBarRect.top);        // Draw a line across
  941.     LineTo((*fDp).port.portRect.right, extScrollBarRect.top);    // The top of the cdev
  942.     
  943.     MoveTo((*fDp).port.portRect.left,extScrollBarRect.bottom-1);        // Draw a line across
  944.     LineTo((*fDp).port.portRect.right, extScrollBarRect.bottom-1);    // The bottom of the cdev
  945.     
  946. {        // HILIGHT THE OKAY BUTTON
  947.     Rect                arect;
  948.     GetDItemFrame(1,&arect);
  949.     PenSize(3,3);InsetRect(&arect,-4,-4);FrameRoundRect(&arect,16,16);PenNormal();
  950. }
  951.     
  952.     
  953.     return true;
  954. }
  955. // see above comments
  956. void t_cdev::DoExtendedView()
  957. {
  958. short            itemHit;
  959. SavedGlobalsPtr    saveGlobs;
  960. Boolean            SetUpWorked;
  961. register    short    err;
  962.  
  963.     saveGlobs = (SavedGlobalsPtr)NewPtr(sizeof(SavedGlobals));
  964.     if (saveGlobs == nil) 
  965.     {
  966.         SysBeep(1);
  967.         return;
  968.     }
  969.     SetUpWorked = ExtendedSetup((Ptr)saveGlobs);
  970.     if (SetUpWorked == false) 
  971.         {    ExtendedClose((Ptr)saveGlobs);return;}
  972.     HLock((Handle)fScrollBar);
  973.     do
  974.     {
  975.     ModalDialog(nil,&itemHit);
  976.     /**** TRACE MASK NAME HIT (scrollable name hit) *****/
  977.     if (itemHit >= fDebugChecks_Start && itemHit <= fDebugChecks_End) 
  978.         {
  979.             DebugMarkHit(itemHit - fDebugChecks_Start);
  980.         }
  981.  
  982.         if (itemHit >=fNames_Start && itemHit <= fNames_End) 
  983.             itemHit = fChecks_Start + (itemHit - fNames_Start);    
  984.         /*
  985.         **        Change itemHit the value of the check box next to that name.
  986.         **        This is so the user can click on the name of the item,
  987.         **        not the exact location of the check box, to toggle that check box.
  988.         */
  989.  
  990.         /****************  CHECK MARK HIT  ********************/    
  991.     
  992.         if (itemHit >= fChecks_Start && itemHit <= fChecks_End) 
  993.             {
  994.                 CheckMarkHit(itemHit);
  995.             }
  996.         switch (itemHit)
  997.             {
  998.                 case     keScrollBarArea:    /***  SCROLL BAR HIT        ***/
  999.                 // GetMouse(&fEvent->where);  (THIS CAUSED ME 1 1/2 DAYS OF ANGUISH!)
  1000.                 // The above line will cause unpredictability if QuickINIT is installed
  1001.  
  1002.                 HandleScrollBar();
  1003.                 break;
  1004.                 case    keClearAll:FillMask(kTraceMaskID, 0);break;
  1005.                 case    keSetAll:FillMask(kTraceMaskID, 1);break;
  1006.                 case    keClearBreakPts:FillMask(kBreakMaskID, 0);break;
  1007.                 case    keClearOnceCheckBox:
  1008.                         fBreakOnceThenClear = !fBreakOnceThenClear;
  1009.                         DSetControl(keClearOnceCheckBox,!fBreakOnceThenClear);
  1010.                         break;
  1011.                 case    keAutoWrite:    
  1012.                         fAutoWriteOn = !fAutoWriteOn;
  1013.                         DSetControl(keAutoWrite,fAutoWriteOn);
  1014.                         break;        
  1015.                 case    keTraceOnStartup: 
  1016.                         fTraceOnStartup = !fTraceOnStartup;
  1017.                         DSetControl (keTraceOnStartup, fTraceOnStartup);
  1018.                         // DoSave(0);
  1019.                         break;
  1020.                 //case    keRevertPrefs:
  1021.                 //        DoSave(true);        // true means delete the prefs...
  1022.                 //        break;
  1023.                 // This is done elsewhere... 
  1024.                 
  1025.                 default    : break;
  1026.             }
  1027.     } while ((itemHit != 1) && (itemHit !=2));
  1028.     
  1029.     if (itemHit == keCancelButton)
  1030.         {
  1031.             DoCopyMask(saveGlobs->TraceMaskCopy,fTraceMask);        // CopyMask(src,dest);
  1032.             DoCopyMask(saveGlobs->BreakMaskCopy,fBreakMask);
  1033.             fBreakOnceThenClear = saveGlobs->BOTCCopy;
  1034.         } 
  1035.     else        // Okay button hit--  handle anything that didn't get handled above..
  1036.         if (saveGlobs->TraceStartup != fTraceOnStartup)
  1037.         {
  1038.             err = Control(fTraceRefNum,kStartedFromInit,(Ptr)fTraceOnStartup);
  1039.             ReportResult(err,kDriverError);    
  1040.             fTraceOnStartup = saveGlobs->TraceStartup;
  1041.         }
  1042.     ExtendedClose((Ptr)saveGlobs);
  1043.     // Now out of extended mode- CdevMaskToDriver will allow us to change these...
  1044.     CdevMaskToDriver(kGetCdevBreakMask);    // Update the breakpoints manually
  1045.     CdevMaskToDriver(kGetCdevTraceMask);    // And also the trace points
  1046.     
  1047.     DisposPtr((Ptr)saveGlobs);
  1048. }
  1049. // see above comments
  1050. void t_cdev::ExtendedClose(Ptr saveGlobsPtr)
  1051. {
  1052. register SavedGlobalsPtr saveGlobs;
  1053.     saveGlobs = (SavedGlobalsPtr)saveGlobsPtr;
  1054.     DisposeControl(fScrollBar);
  1055.     fScrollBar = saveGlobs->ScrollBarControl;
  1056.     DisposDialog((GrafPtr)fDp);
  1057.     fDp = saveGlobs->DP;
  1058.     fMaskIndex = saveGlobs->MaskIndex;
  1059.     fLastItem = saveGlobs->LastItem;
  1060.     fInCDEVMode = true;        // Leaving external modal dialog..
  1061.     
  1062.     
  1063.     // SemiConstants reset to small scroll bar environment.
  1064.     fFieldsPerBank = kFieldsPerBank;
  1065.     fMinBankSize = kMinBankSize;
  1066.     fMaxBankSize = kMaxBankSize;
  1067.     fChecks_Start = iChecks_Start;
  1068.     fChecks_End = iChecks_End;
  1069.     fNames_Start = iNames_Start;
  1070.     fNames_End = iNames_End;
  1071.     fDebugChecks_Start = iDebugChecks_Start;
  1072.     fDebugChecks_End = iDebugChecks_End;
  1073.     fMaskNumStatic = iMaskNumStatic;
  1074.     fErrorMsgStatic = iErrorMsgStatic;
  1075.     fErrorNumberStatic = iErrorNumberStatic;
  1076.  
  1077.     if (saveGlobs->InitialTraceState == true)
  1078.         EnableTrace(true);
  1079.     if (saveGlobs->AutoWriteOn != fAutoWriteOn)
  1080.     {
  1081.         // AutoWrite button clicked...
  1082.         fAutoWriteOn = !fAutoWriteOn; // change back..
  1083.         ToggleAutoWrite();        // and let ToggleAutoWrite change fAutoWrite and all
  1084.     }
  1085.     Activate();        // Reactivate the cdev's small scroll bar...
  1086.     UpdateFields();
  1087. }
  1088.  
  1089. //
  1090. //    On Entry, WhichMask should equal either kBreakMaskID or kTraceMaskID (the target)
  1091. //    If SetCondition == true, then target Mask is set to all 1's.
  1092. //    (else, target mask is cleared-- ie no tracepoints set)
  1093. //
  1094.  
  1095. void    t_cdev::FillMask(short WhichMask, Boolean SetCondition)
  1096. {
  1097. register        lhex    x;
  1098. register        snum    result;
  1099.  
  1100.     if (WhichMask == kBreakMaskID)
  1101.         {
  1102.         if (SetCondition == true)    
  1103.             for (x=0;x<kBitsPerMask;x++)
  1104.                 BitSet((Ptr)fBreakMask,x);        
  1105.         else
  1106.             for (x=0;x<kBitsPerMask;x++)
  1107.                 BitClr((Ptr)fBreakMask,x);
  1108.         }
  1109.     else
  1110.         {
  1111.         if (SetCondition == true)    
  1112.             for (x=0;x<kBitsPerMask;x++)
  1113.                 BitSet((Ptr)fTraceMask,x);
  1114.         else
  1115.             for (x=0;x<kBitsPerMask;x++)
  1116.                 BitClr((Ptr)fTraceMask,x);
  1117.         }
  1118.     result = CdevMaskToDriver(kGetCdevTraceMask);        
  1119.     ReportResult(result,kDriverError);
  1120.     UpdateFields();
  1121. }
  1122.  
  1123. char *FindNextPStr(char *string)
  1124. {
  1125. char len;
  1126.     len = string[0];        
  1127.     return (string + len + 1);
  1128. }
  1129.  
  1130. void t_cdev :: GetFieldName(char *itemName, short index)
  1131. {
  1132. register short x;
  1133. char *namePtr, len;
  1134. short *numStrings;
  1135. char     fakeName[100];
  1136.     if (fFieldNames == nil)
  1137.         *itemName = 0;
  1138.     else
  1139.     {
  1140.         HLock(fFieldNames);            //  This should be taken out...
  1141.         numStrings = (short *)*fFieldNames;
  1142.         if ((index) >= *numStrings) 
  1143.         {
  1144.             NumToString(index, (Str255)fakeName);
  1145.             namePtr = fakeName;
  1146.         }
  1147.         else
  1148.         {
  1149.         namePtr = *fFieldNames;
  1150.         namePtr += 2;    // Skip past the # of Strings word..
  1151.     
  1152.             for (x=0;x<index;x++)
  1153.                 namePtr = FindNextPStr(namePtr);
  1154.         }
  1155.         
  1156.         len = namePtr[0];
  1157.         for (x=0;x<len+1;x++)
  1158.         {
  1159.             itemName[x] = namePtr[x];    
  1160.         }
  1161.         HUnlock(fFieldNames);
  1162.     }
  1163. }
  1164. void    t_cdev::DGetString(short    item,Str255    str)
  1165. {
  1166. short    ItemType;
  1167. Handle    ItemHandle;
  1168. Rect    ItemRect;
  1169.  
  1170.     GetDItem((GrafPort *)fDp, fLastItem + item  , &ItemType, &ItemHandle, &ItemRect);
  1171.     SetIText(ItemHandle, str);
  1172. }
  1173.  
  1174. void    t_cdev::HandleScrollBar(void)
  1175. {
  1176. Point where;
  1177. short    part,oldValue,returnCode,newValue;
  1178. register ControlHandle    control;    
  1179.     // Get the location (in LOCAL coords) of where the mouse was.
  1180.  
  1181.     if (!fInCDEVMode) GetMouse(&where);
  1182.     else
  1183.     {
  1184.     where = fEvent->where;
  1185.     GlobalToLocal(&where);
  1186.     }
  1187.     
  1188.     
  1189.     if ((fScrollBar == nil) || (*fScrollBar == nil))     // This shouldnt happen
  1190.     {
  1191.         DebugStr("\pcdev Sbar == 0L!");  
  1192.         return;
  1193.     }
  1194.     oldValue = GetCtlValue(fScrollBar);
  1195.  
  1196.     SetCRefCon(fScrollBar, (long)this );    // Lets our non-object filter get obj stuff
  1197.     part = FindControl(where,(GrafPort *)fDp,&control);        /* We know this is fScrollBar */
  1198.     if (control != nil) 
  1199.         {
  1200.         if (part == inThumb) 
  1201.             returnCode = TrackControl(control,where,(ProcPtr)0L);    
  1202.         else 
  1203.             returnCode = TrackControl(control,where,(ProcPtr)ScrollFilter);
  1204.         newValue = GetCtlValue(fScrollBar);
  1205.         if (oldValue != newValue) 
  1206.             ChangeBank(newValue);
  1207.         }
  1208. }
  1209.  
  1210.  
  1211. // The number of bytes buffered changes as data is logged and or written to a file.
  1212. // The cdev needs to make these changes apparent.  
  1213. // Also: Now the trace can be turned on and off externally through a dcmd and maybe later
  1214. // through a VBL task type FKEY...  
  1215. //  The cdev needs to be aware of possible changes and update it's varibles accordingly.
  1216.  
  1217. void    t_cdev::Idle()
  1218. {
  1219. register    short                    result;
  1220. register    long    breakMaskCopy[4];
  1221.     if (!fInCDEVMode) return;
  1222.     
  1223.     result = UpdateTraceStatusBlk();
  1224.     if (result == noErr)
  1225.         {
  1226.             if (fBytesBuffered != fTraceStatus.bytesBuffered) 
  1227.                 {
  1228.                     fBytesBuffered = fTraceStatus.bytesBuffered;
  1229.                     if (fInCDEVMode) DSetLong(iBytesBuffedStatic,fBytesBuffered);
  1230.                 }
  1231.             if (fBytesWritten != fTraceStatus.bytesWritten) 
  1232.                 {    /* Display # of bytes written to disk file */
  1233.                     fBytesWritten = fTraceStatus.bytesWritten;
  1234.                     if (fInCDEVMode) DSetLong(iFileSizeStatic,fBytesWritten);    
  1235.                 }
  1236.             if (fTraceStatus.DebugMarkUnset)
  1237.                 {
  1238.                     DriverToCdevMask(kSendCdevBreakMask);    // Get real mask (may or may not be any different 
  1239.                     UpdateFields();
  1240.                 }
  1241.             if (fTraceStatus.online != fTraceEnabled)
  1242.                 SetOnOff(fTraceStatus.online);
  1243.  
  1244.         }
  1245.     /* We dont call ReportResult here since this is called repeatedly    */
  1246. }
  1247.  
  1248. OSErr    t_cdev::Init()
  1249. {
  1250. register    snum    Err = noErr,a;
  1251.  
  1252.  
  1253.     SysEnvirons(curSysEnvVers, &fEnvironment);    
  1254.     
  1255.     // SemiConstants-- these only change when going to the extended dialog...
  1256.     fFieldsPerBank = kFieldsPerBank;
  1257.     fMinBankSize = kMinBankSize;
  1258.     fMaxBankSize = kMaxBankSize;
  1259.     fChecks_Start = iChecks_Start;
  1260.     fChecks_End = iChecks_End;
  1261.     fNames_Start = iNames_Start;
  1262.     fNames_End = iNames_End;
  1263.     fDebugChecks_Start = iDebugChecks_Start;
  1264.     fDebugChecks_End = iDebugChecks_End;
  1265.     fInCDEVMode = true;
  1266.     fMaskNumStatic = iMaskNumStatic;
  1267.     fErrorMsgStatic = iErrorMsgStatic;
  1268.     fErrorNumberStatic = iErrorNumberStatic;
  1269.     
  1270.     fScrollBar = 0L;
  1271.     SetPort((GrafPtr)fDp);
  1272.     fStatus = (long) this;    /* So we can get back at our class, globals etc.. from main */
  1273.     // Assign possible sizes for the buffer....  
  1274.  
  1275.     a=0;        // Do not have more then kMaxNumSizes
  1276.  
  1277. // set the possible sizes for the buffer 
  1278.     fBufSizesHandle[a++] = 0  * 1024;
  1279.     fBufSizesHandle[a++] = 4  * 1024;
  1280.     fBufSizesHandle[a++] = 8  * 1024;
  1281.     fBufSizesHandle[a++] = 16  * 1024;
  1282.     fBufSizesHandle[a++] = 32  * 1024;
  1283.     fBufSizesHandle[a++] = 96  * 1024;
  1284.     fBufSizesHandle[a++] = 256  * 1024;
  1285.     fBufSizesHandle[a++] = 512  * 1024;
  1286.     fBufSizesHandle[a++] = 1024  * 1024;
  1287.     fBufSizesHandle[a++] = 2024  * 1024;
  1288.     fBufSizesHandle[a++] = 4024  * 1024;
  1289.     fBufSizesHandle[a++] = 8024  * 1024;    // Virtual memory support?
  1290.  
  1291.     fBufferNumSizes = a-1;
  1292.     
  1293.     fBugPicture = GetPicture(kBugPictureID);
  1294.         if (fBugPicture == nil) DebugStr("\pInitPictFail");    // This should close the cdev..
  1295.     
  1296.     /* Get and position Scroll Bar */
  1297.     /* Get the size and location of scroll bar (from a user item template) */
  1298.  
  1299.     GetDItemFrame(iScrollBarUserItem,&fSBarRect);        /* Get area of scroll bar (a user item ditl) */
  1300.         
  1301.     fScrollBar = NewControl((GrafPort *) fDp,&fSBarRect,"\p",true,0,fMinBankSize,
  1302.         fMaxBankSize,scrollBarProc,0L);    /* And create a scroll bar of that size */
  1303.     if (fScrollBar == nil) DebugStr("\pInitSbarFail");
  1304.     ChangeBank((short) 0);        /* Set fMaskIndex to zero and draw Mask Names */
  1305.  
  1306.     DoLoad();  // load user prefs
  1307.  
  1308.     Err = TraceInit();    
  1309.     ReportResult(Err,kDriverError);
  1310.     
  1311.  
  1312.     return (Err);
  1313. }
  1314.  
  1315. // Handle control item hit
  1316. void    t_cdev::ItemHit(short itemHit)
  1317. {
  1318. register short origValue, start, x, bank;
  1319. register short result;
  1320.  
  1321.     if (itemHit >= fDebugChecks_Start && itemHit <= fDebugChecks_End) 
  1322.         {
  1323.             DebugMarkHit(itemHit - fDebugChecks_Start);
  1324.         }
  1325.  
  1326.     /**** TRACE MASK NAME HIT (scrollable name hit) *****/
  1327.  
  1328.     if (itemHit >=fNames_Start && itemHit <= fNames_End) 
  1329.         {    
  1330.             itemHit = fChecks_Start + (itemHit - fNames_Start);    
  1331.         /*
  1332.         **        Change itemHit the value of the check box next to that name.
  1333.         **        This is so the user can click on the name of the item,
  1334.         **        not the exact location of the check box, to toggle that check box.
  1335.         */
  1336.         }
  1337.         
  1338.         
  1339.     /****************  CHECK MARK HIT  ********************/    
  1340.     
  1341.     if (itemHit >= fChecks_Start && itemHit <= fChecks_End) 
  1342.         {
  1343.             CheckMarkHit(itemHit);
  1344.             return;
  1345.         }
  1346.     start = 128;
  1347.     bank = 32;        // Clicking on a button will switch to the start of that group of fields...
  1348.     switch (itemHit)
  1349.         {
  1350.             case     iScrollBarUserItem:HandleScrollBar();break;    /***  SCROLL BAR HIT        ***/
  1351.             
  1352.             case    iConfigFileButton:    SetName();break;
  1353.             case    24:    start -= 32;bank -=8;    // Level 4            
  1354.             case    41:    start -= 32;bank -=8;    // Level 3
  1355.             case    42: start -= 32;bank -=8;    // Level 2
  1356.             case    43: start -= 32;bank -=8;    // Level 1
  1357.                     // This will set or clear the groups of 32.  
  1358.                     // Group one == 0-31, 2 == 32-63, etc to group 4.
  1359.                     origValue = BitTst((Ptr)fTraceMask, start);
  1360.                     if (origValue == true)
  1361.                         for (x=0;x<32;x++)
  1362.                             BitClr((Ptr)fTraceMask, x + start);
  1363.                     else
  1364.                         for (x=0;x<32;x++)
  1365.                             BitSet((Ptr)fTraceMask, x + start);
  1366.                     result = CdevMaskToDriver(kGetCdevTraceMask);        
  1367.                     ReportResult(result,kDriverError);
  1368.                     UpdateFields();    
  1369.                     // This switches the scroll fields...  it may be a little annoying...
  1370.                     if (origValue == false)
  1371.                         ChangeBank(bank);
  1372.                     break;
  1373.     
  1374.             case    iOnChk:
  1375.             case    iOnMsg:     if (!fTraceEnabled) EnableTrace(true);break;
  1376.             case    iOffChk:
  1377.             case    iOffMsg: if (fTraceEnabled) EnableTrace(false);break;
  1378.             
  1379.             case    iWriteBufferButton:WriteBufferNow();break;
  1380.             case    iClearBufferButton:ClearBufferNow();break;
  1381.             
  1382.             case    iAutoWriteChk:
  1383.             case    iAutoWriteMsg:ToggleAutoWrite();break;
  1384.             case    iResetEOFButton:ResetEOF();break;
  1385.             
  1386.             case    iSizeIncrement:    /* Only allow size changes when off */
  1387.             case    iSizeDecrement: 
  1388.                     //if (!fTraceEnabled) 
  1389.                     if (TrackItem(itemHit)) SetSize(itemHit);
  1390.                     
  1391.                     break;
  1392.                 
  1393.             case     iBkptEllipsisButton:DoExtendedView();break;        /* Show Help Dialog */    
  1394.             case    iMaskEllipsisButton:    // RESET THE TRACE!
  1395.                 if (fTraceEnabled) 
  1396.                     EnableTrace(false);    // disable trace
  1397.                 fCurrentSizeIndex = kSizeIndexDefault;
  1398.                 EnableBuffer(true);
  1399.                 fTraceOnStartup = false;    // turn off trace from startup
  1400.                 fBreakOnceThenClear = true;    // will get sent to drvr by FillMask
  1401.                 (void) Control(fTraceRefNum,kStartedFromInit,(Ptr)fTraceOnStartup);
  1402.                 FillMask(kBreakMaskID, 0);    // Clear Break Mask
  1403.                 FillMask(kTraceMaskID, 0);    // and trace mask
  1404.                 if (fAutoWriteOn) ToggleAutoWrite();    // turn off periodic write
  1405.                 
  1406.                 break;
  1407.             default    : break;
  1408.         }
  1409. }
  1410.  
  1411. // This is where we would handle key presses
  1412. void    t_cdev::Key(short )
  1413. {
  1414.  
  1415. }
  1416.  
  1417. // Note:  Assumes NewPtrClear was used to create globals..
  1418.  
  1419.  
  1420. // This is 2 bytes for 0-9, 3 for 10-99, 4 for 100-127
  1421. //#define kFieldHandleSize ((10L * 2L) + (90L * 3L) + (27L * 4L))
  1422. #define kFieldHandleSize 500L  // close enough
  1423.  
  1424. Boolean    t_cdev::LoadFieldNames()
  1425. {
  1426. register long x;
  1427. register Boolean result = true;
  1428. short PrefsResRefNum = 0;
  1429. char len, *pos;
  1430. short err;
  1431.  
  1432.     if (fFieldNames != nil) 
  1433.         DisposHandle(fFieldNames);
  1434.  
  1435.     fFieldNames = GetResource('STR#', kExternalNamesListID);
  1436.     err = ResError();
  1437.     if (fFieldNames == nil)    // We need to create a list of fields for the user.
  1438.         {
  1439.             fFieldNames = NewHandleClear(kFieldHandleSize);        
  1440.             if (fFieldNames == nil) 
  1441.             {
  1442.                 // we're in deep trouble...  
  1443.                 result = false;    
  1444.             }
  1445.             else
  1446.             {
  1447.             pos = *fFieldNames;        // Create a STR# list of integers 0-127
  1448.             pos[0] = 0;        // Create a length word...
  1449.             pos[1] = 128;    // 0x0080 pascal strings...
  1450.             pos+=2;    // skip past # word...
  1451.             for (x=0;x<128L;x++)
  1452.                 {
  1453.                     NumToString(x, (Str255)pos);
  1454.                     len = *pos;
  1455.                     pos += len+1;    // skip past the length byte and the length of the string
  1456.                 }
  1457.             }
  1458.         }
  1459.     else        // Just detach the names...
  1460.         DetachResource(fFieldNames);        
  1461.     return result;
  1462. }
  1463.  
  1464. // cdev message dispatching
  1465. long t_cdev::Message(short msg, short item)
  1466. {
  1467. register    long result;
  1468. register    snum    err;
  1469.  
  1470.     result = 1;
  1471.     switch (msg) {
  1472.         case initDev:
  1473.             err = Init();
  1474.             break;
  1475.         case hitDev:
  1476.             ItemHit(item - fLastItem);
  1477.             break;
  1478.         case closeDev:
  1479.             Close();
  1480.             result = 0;    /* Signal that we are done... */
  1481.             break;
  1482.         case nulDev:
  1483.             Idle();
  1484.             break;
  1485.         case updateDev:
  1486.             Update();
  1487.             break;
  1488.         case activDev:
  1489.             Activate();
  1490.             break;
  1491.         case deactivDev:
  1492.             Deactivate();
  1493.             break;
  1494.         case keyEvtDev:
  1495.             if (!(fEvent->modifiers & cmdKey))
  1496.                 Key((unsigned char) fEvent->message);
  1497.             else if (fEvent->message != autoKey)
  1498.                 CmdKey((unsigned char) fEvent->message);
  1499.             break;
  1500.         case undoDev:
  1501.             ;
  1502.             break;
  1503.         case cutDev:
  1504.             ;
  1505.             break;
  1506.         case copyDev:
  1507.             ;
  1508.             break;
  1509.         case pasteDev:
  1510.             ;
  1511.             break;
  1512.         case clearDev:
  1513.             ;
  1514.             break;
  1515.     }
  1516.  
  1517.     return(result);
  1518. }
  1519.  
  1520.  
  1521. /*
  1522. **
  1523. **     ReportResult looks at the result field, and if there is an error (result != 0)
  1524. **    it reports it to the user.  It looks in the MsgNum to find the type of problem...
  1525. **    The MsgNum has the Str# equates.  
  1526. **    Note:  If there wasn't an error, we do not report the kind of error, but use
  1527. **    the kNoError, which is either blank or "noErr", depending on the STR#.
  1528. **
  1529. **
  1530. */
  1531. void    t_cdev::ReportResult(short    result,short    MsgNum)
  1532. {
  1533. Str255        ErrorStr;
  1534.     // if (fInCDEVMode == false) return;    // No Error Reporting while in extended mode...
  1535.     if (result == 0) /* No Error Condition */
  1536.         {
  1537.             GetIndString(ErrorStr,kTraceErrorsID,kNoErrMsg);
  1538.             DSetString(fErrorMsgStatic,ErrorStr);    /* Clear Error field or put up noErr msg... */
  1539.             DSetLong(fErrorNumberStatic,(long)result);
  1540.         }
  1541.     else    
  1542.         {
  1543.     
  1544.             GetIndString(ErrorStr,kTraceErrorsID,MsgNum);
  1545.             if (result == kInternalMsg)        /* Internal Error is Blue... */
  1546.                 {
  1547.                     ForeColor(blueColor);
  1548.                     DSetLong(fErrorNumberStatic,0L);    
  1549.                 }
  1550.             else    /* A driver/OS/file Error has occurred... */
  1551.                 {        
  1552.                     ForeColor(redColor);    /* Let em know in red.. */
  1553.                     DSetLong(fErrorNumberStatic,(long)result);    
  1554.                 }
  1555.                 
  1556.             DSetString(fErrorMsgStatic,ErrorStr);
  1557.             SysBeep(1);
  1558.  
  1559.             result = 0;        /* Makes it easy to spot this from asm. */
  1560.  
  1561.             ForeColor(blackColor);
  1562.         }
  1563. }
  1564.  
  1565. // resets data file-- currently this is "Tracks Prefs"
  1566. void    t_cdev::ResetEOF()
  1567. {
  1568. register snum result;    
  1569.     result = Control(fTraceRefNum, kResetEOF, NULL);
  1570.     ReportResult(result,kFileError);
  1571. }
  1572.  
  1573.  
  1574. /*    ScrollFilter not included in the class definition, 
  1575. **  since filters cant be C++ based..
  1576. */
  1577. pascal    void    ScrollFilter(ControlHandle    SBar,short    partCode)
  1578. {
  1579. register    t_cdev    *Obj;
  1580. register    PageAmount;
  1581.     Obj = (t_cdev*) GetCRefCon(SBar);
  1582.     if (Obj->fKey == kOurKey)         // If fkey != kOurKey, the scroll bar has become invalid... dont handle
  1583.     {
  1584.         if (Obj->fInCDEVMode)
  1585.             PageAmount = 4;        
  1586.         else
  1587.             PageAmount = 1;    // It's in Extended Dialog- so larger sbar wants smaller page amount
  1588.         switch (partCode)
  1589.             {
  1590.                 case    inUpButton:Obj->ChangeBank  (Obj->fMaskIndex -1);break;
  1591.                 case    inDownButton:Obj->ChangeBank(Obj->fMaskIndex +1);break;
  1592.                 case    inPageUp:Obj->ChangeBank    (Obj->fMaskIndex -PageAmount);break;
  1593.                 case    inPageDown:Obj->ChangeBank  (Obj->fMaskIndex +PageAmount);break;
  1594.                 case    inThumb:;break;
  1595.                 default    :break;
  1596.             }
  1597.         }
  1598.     return;
  1599. }
  1600. // put a signed decimal long  into a dialog item
  1601. void    t_cdev::DSetLong(short    item,long    value)
  1602. {
  1603. Str255    NumStr;
  1604.  
  1605.     NumToString(value,NumStr);
  1606.     DSetString(item,(Str255)NumStr);
  1607. }
  1608.  
  1609. // Changes the name of the driver that is being traced. 
  1610. // The driver that is to be traced is selected by SFGetFile and that file is opened,
  1611. // and two resources are looked for by GetTargAppStuff().  
  1612.  
  1613. // A thought was given to the idea of saving the working directory id in the user
  1614. // prefs, so a driver can be in the Extensions folder with system seven...  
  1615. // This has not been done.  Any file that is used to get the name of the driver and 
  1616. // the string list needs to be in the system folder.  
  1617.  
  1618. void    t_cdev::SetName() 
  1619. {
  1620. register SFReply                reply;
  1621. Point                            wh;
  1622. StringHandle                    NewFilePrompt;
  1623. char                            FileName[32];
  1624. OSType                            typeList[4];
  1625. short                            typeCount;
  1626. unsigned char sizeByte;
  1627.  
  1628.  
  1629.     SetPt(&wh,100,40);        /* User interface guide!! */
  1630.     /* Get a file name and vRefNum from user and pass to Enable File.. */
  1631.     
  1632.     typeList[1] = 'INIT';
  1633.     typeList[0] = 'cdev';
  1634.     typeList[2] = 'rsrc';    // lets user create a resource file with the needed info..
  1635.     
  1636.     // Any file that could have a 'DRVR' resource in it....
  1637.     // or a user file with a kDRp resource and a stringlist....
  1638.     
  1639.     typeCount = 3;
  1640.     NewFilePrompt = GetString(-4064);        //  
  1641.     HLock((Handle)NewFilePrompt);
  1642.     // Would be nice if it opened to the system Folder
  1643.     SetVol(nil, fEnvironment.sysVRefNum);        
  1644.  
  1645.     SFGetFile(wh, (const Str255)*NewFilePrompt, nil, typeCount, typeList, nil, &reply);
  1646.         
  1647.     HUnlock((Handle)NewFilePrompt);
  1648.  
  1649.     if (reply.good)
  1650.         {
  1651.  
  1652.             if (fTraceEnabled) 
  1653.                 EnableTrace(false);        // turn trace off...
  1654.             sizeByte = *reply.fName;
  1655.             if (fTargetAppName != nil)
  1656.             {
  1657.                 DisposHandle(fTargetAppName);
  1658.             }
  1659.             fTargetAppName = NewHandle((long)sizeByte + 1);
  1660.             BlockMove((Ptr)reply.fName, (Ptr)*fTargetAppName, (long)sizeByte+1);
  1661.             GetTargAppStuff();
  1662.             
  1663.             UpdateFields();
  1664.             
  1665.             // InvokeTrace??
  1666.         }
  1667. }
  1668.  
  1669. /* Sets On Off button of CDEV-- called when Tracks on/off status changes */
  1670. void    t_cdev::SetOnOff(Boolean    On)        
  1671. {
  1672.     DSetControl(iOnChk,On);
  1673.     DSetControl(iOffChk,!On);
  1674.     fTraceEnabled = On;    
  1675. }
  1676.  
  1677. // Change Buffer Size
  1678. void    t_cdev::SetSize(short    whichHit)
  1679. {
  1680. OSErr    err;
  1681. register short    saveIndex;
  1682. register lhex    saveSize;
  1683.     if (fCurrentSizeIndex & 0xFF00)    {fCurrentSizeIndex = 3;}    // make sure size index is in check...
  1684.  
  1685.     
  1686.     if ((fBytesBuffered !=0) || fTraceEnabled) 
  1687.         ReportResult(kInternalMsg,kBufferNotClear);
  1688.     else
  1689.         {
  1690.             saveSize = fBufferSize;
  1691.             saveIndex = fCurrentSizeIndex;
  1692.             
  1693.             if (whichHit == iSizeIncrement)
  1694.                 if (fCurrentSizeIndex < fBufferNumSizes)
  1695.                     fCurrentSizeIndex++; 
  1696.                 else {fCurrentSizeIndex = fBufferNumSizes;}
  1697.         
  1698.             if (whichHit == iSizeDecrement)
  1699.                 if (fCurrentSizeIndex > 0)
  1700.                     fCurrentSizeIndex--;
  1701.                 else {fCurrentSizeIndex = 0;}
  1702.                 
  1703.             err = EnableBuffer(true);        // Parameters in fCurrentSizeIndex
  1704.             if (err !=noErr)    // Try to reset it to what it was...
  1705.                 {                        
  1706.                     fCurrentSizeIndex = fCurrentSizeIndex;        // Restore- if pos
  1707.                     err = EnableBuffer(true);
  1708.                     if (err) SysBeep(1);    // afford to use stronger language here..
  1709.                     fCurrentSizeIndex = 0;    // Reset to zero.. this might help
  1710.                 }
  1711.                 (void) EnableBuffer(true);
  1712.             }
  1713. }
  1714.  
  1715.  
  1716. //    Draws text to a dialog.  fLastItem is the offset to the DITL (if in a cdev)
  1717. //     This will draw the text even if the window is not foremost.  
  1718. //
  1719. void    t_cdev::DSetString(short    item, Str255    str)
  1720. {
  1721. Handle            StrHandle;
  1722. WindowPeek     saveList;
  1723. Rect    ItemRect;
  1724. short    ItemType;
  1725.  
  1726.     GetDItem((GrafPort *)fDp, fLastItem + item  , &ItemType, &StrHandle, &ItemRect);
  1727.     if (StrHandle == nil) 
  1728.         {return;} 
  1729.     else 
  1730.     if (fInCDEVMode)        // Move the window to the front, then put back when drawn
  1731.     {
  1732.         saveList = *((WindowPeek*)WindowList);
  1733.         *((WindowPeek*)WindowList)  = fDp;
  1734.         fDp->windowKind = dialogKind;
  1735.         SetPort((GrafPort *)fDp);
  1736.         SetIText(StrHandle, str);        
  1737.         fDp->windowKind = fWindRefnum;
  1738.         *((WindowPeek*)WindowList) = saveList;
  1739.     }
  1740.     else
  1741.     {
  1742.     SetIText(StrHandle, str);    
  1743.     }
  1744.     
  1745. }
  1746. // Sends message to Tracks driver to turn periodic write to file on
  1747. OSErr        t_cdev::ToggleAutoWrite()
  1748. {
  1749. short    result;
  1750.  
  1751.     fAutoWriteOn = !fAutoWriteOn; 
  1752.     if (fAutoWriteOn)
  1753.         result = Control(fTraceRefNum, kSetAutoWriteOn, nil);    // param -> nil
  1754.     else
  1755.         result = Control(fTraceRefNum, kSetAutoWriteOff, nil);
  1756.     DSetControl(iAutoWriteChk, fAutoWriteOn);
  1757.     ReportResult(result,kDriverError);
  1758.     return result;
  1759. }
  1760. // Resets the trace.
  1761. snum    t_cdev::TraceInit()
  1762. {
  1763. snum result;
  1764. short prefsVRef;
  1765.  
  1766.       fKey = kOurKey;    // this is to identify our globals
  1767.  
  1768.     result = OpenDriver("\p.TRACE", &fTraceRefNum);
  1769.     if (result != noErr) 
  1770.         {
  1771.             return result;    /* Escape now, while you can... */
  1772.         }
  1773.  
  1774.     result = UpdateTraceStatusBlk();    // Updates fTraceStatus
  1775.     if (result == 0)                /* Set Dialog Items    & Enable Buffer and Output file    */
  1776.         {
  1777.     /******************  SET UP BUFFER  *******************/
  1778.         if ((!fTraceStatus.bufferEnabled))
  1779.             {
  1780.                 fCurrentSizeIndex = kSizeIndexDefault;
  1781.                 EnableBuffer(true);
  1782.                 result =  UpdateTraceStatusBlk();
  1783.                 /* we need another account of what the buffersize is */
  1784.             }
  1785.         fCurrentSizeIndex = fTraceStatus.bufferSizeIndex;        /* Index to size of buffer */
  1786.         DSetLong(iBufferSizeStatic,fTraceStatus.bufferSize);
  1787.         
  1788.     /****************** IS IT ON LINE?? ******************/
  1789.         SetOnOff(fTraceStatus.online);    /* Enable correct ditem for on/off of Trace */
  1790.         
  1791.     /****************** CHECK THE TRACE OUTPUT FILE ****************/
  1792.         prefsVRef = GetFolderVol(kPreferencesFolderType);
  1793.         if (prefsVRef == 0) 
  1794.             prefsVRef = fEnvironment.sysVRefNum;
  1795.             
  1796.         EnableFile((char *)kDefaultFileName, prefsVRef);        // EnableFile is more like a SetFileName.  
  1797.         
  1798.     /***************** Display Status of Periodic Time Option ************/
  1799.         fAutoWriteOn = fTraceStatus.autoWrite;        /* Set Global to current state     */
  1800.         DSetControl(iAutoWriteChk,fAutoWriteOn);    /* Set AutoWrite checkbox             */
  1801.         
  1802.     /***************** Get Mask & Set appropriate check boxes     *******************/
  1803.  
  1804.         DriverToCdevMask(kSendCdevTraceMask);    
  1805.         DriverToCdevMask(kSendCdevBreakMask);    
  1806.         fBreakOnceThenClear = fTraceStatus.breakOnceThenClear;
  1807.  
  1808.         // these will be reset...
  1809.         fBufferSize = fTraceStatus.bufferSize;
  1810.         fBytesBuffered = 0L;        
  1811.         DSetLong(iFileSizeStatic,fBytesWritten);DSetLong(iBytesBuffedStatic,fBytesBuffered);    
  1812.  
  1813.         }    /* End Result = 0 */
  1814.     UpdateFields();
  1815.     
  1816.     return result;        
  1817. }
  1818.  
  1819. // this hilights a pseudo button
  1820. Boolean    t_cdev::TrackItem(int    item)
  1821. {
  1822. Rect        ItemFrame;
  1823. Boolean    Inverted=true,InRect;
  1824. Point        wh;
  1825.     GetDItemFrame(item,&ItemFrame);
  1826.     InvertRect(&ItemFrame);
  1827.     while (Button())
  1828.         {
  1829.             GetMouse(&wh);
  1830.             InRect = PtInRect(wh,&ItemFrame);
  1831.             if ((InRect && !Inverted) || (!InRect && Inverted))
  1832.                 {
  1833.                     InvertRect(&ItemFrame);
  1834.                     Inverted = !Inverted;
  1835.                 }
  1836.         }
  1837.     if (Inverted)
  1838.         InvertRect(&ItemFrame);        /* Don't leave Button hilighted (inverted) */
  1839.     return (Inverted);        /* If previously inverted, then it was hit and released within */
  1840. }
  1841.  
  1842. // respond to an update event
  1843. void    t_cdev::Update(void)        /*  "updateDev"  */
  1844. {
  1845.     /* Draw thin line above the Scroll bar... */    
  1846.     if (fInCDEVMode) 
  1847.     {
  1848.         MoveTo(87,fSBarRect.top);
  1849.         LineTo(304,fSBarRect.top);
  1850.         UpdateFields();        // And fill in all user items and fields
  1851.     }
  1852. //    Updates for Extended mode are ignored.
  1853. }
  1854.  
  1855.  
  1856. /*     This function fills in the X's in the dialog for the bank index.    */    
  1857. /*     bankvalue has the bit pattern for the current fields                 */
  1858. /*        It also fills in the mask names in the right spaces...             */
  1859.  
  1860. void    t_cdev::UpdateFields(void)        
  1861. {
  1862. register shex            FieldCount;
  1863. register short            index;
  1864. Str255                    ItemName;
  1865. register Boolean        IsSet;
  1866. Rect    r;                    // For hilighting the Trace Level (1-4)
  1867.  
  1868.     index = fMaskIndex * fFieldsPerBank;        /* Which Mask # from 0-127    */
  1869.     if (index >= kBitsPerMask) // won't happen, but just in case... 
  1870.     {
  1871.         fMaskIndex = 0;
  1872.         return;
  1873.     }    
  1874.     SetPort((GrafPort *)fDp);
  1875.     for (FieldCount=0;FieldCount<fFieldsPerBank;FieldCount++)
  1876.         {    
  1877.             /* Update the Check Marks */
  1878.             
  1879.             IsSet = BitTst((Ptr)fTraceMask,index+FieldCount);
  1880.             DSetControl(fChecks_Start + FieldCount,IsSet);    
  1881.             
  1882.             /*        Now Update the Mask Names... */
  1883.             GetFieldName((char *)ItemName, index+FieldCount);    
  1884.             DSetString(fNames_Start+FieldCount, ItemName);    
  1885.  
  1886.             /*         And finally, Update the Bug Symbols */
  1887.             IsSet = BitTst((Ptr)fBreakMask,index+FieldCount);
  1888.             DrawABug(FieldCount,IsSet);
  1889.  
  1890.         }    
  1891.     DSetLong(fMaskNumStatic,(long)fMaskIndex * fFieldsPerBank);// Which mask num they are on
  1892.     if (GetCtlValue(fScrollBar) != fMaskIndex)
  1893.         SetCtlValue(fScrollBar,fMaskIndex);
  1894.     if (fInCDEVMode)
  1895.         if (fDriverDotName != nil)
  1896.             DSetString(iFileNameStatic,(StringPtr)*fDriverDotName);
  1897.         else 
  1898.         {    // this handles rare occurances when setup fails to get a driver name to trace..
  1899.             fDriverDotName = GetResource('STR ', -4062);    // Won't fail...
  1900.             DSetString(iFileNameStatic,(StringPtr)*fDriverDotName);
  1901.         }
  1902.         
  1903.     // Hilight the proper trace level buttons (1-4)
  1904.     if (fInCDEVMode)
  1905.     {
  1906.         if (!BitTst((Ptr)fTraceMask, 0))    ForeColor(whiteColor); else ForeColor(greenColor);
  1907.         GetDItemFrame(iLevel1Button, &r);
  1908.         InsetRect(&r, -2, -2);
  1909.         FrameRoundRect(&r,8,8);
  1910.         
  1911.         if (!BitTst((Ptr)fTraceMask, 32))    ForeColor(whiteColor); else ForeColor(greenColor);
  1912.         GetDItemFrame(iLevel2Button, &r);
  1913.         InsetRect(&r, -2, -2);
  1914.         FrameRoundRect(&r,8,8);
  1915.         
  1916.         if (!BitTst((Ptr)fTraceMask, 64))    ForeColor(whiteColor); else ForeColor(greenColor);
  1917.         GetDItemFrame(iLevel3Button, &r);
  1918.         InsetRect(&r, -2, -2);
  1919.         FrameRoundRect(&r,8,8);
  1920.         
  1921.         if (!BitTst((Ptr)fTraceMask, 96))    ForeColor(whiteColor); else ForeColor(greenColor);
  1922.         GetDItemFrame(iLevel4Button, &r);
  1923.         InsetRect(&r, -2, -2);
  1924.         FrameRoundRect(&r,8,8);
  1925.         ForeColor(blackColor);    // set color back to black...
  1926.     }
  1927.     
  1928. }
  1929.  
  1930. // Gets status of driver... used to detect changes that were not caused from
  1931. // within the cdev-- eg through a dcmd.
  1932. snum    t_cdev::UpdateTraceStatusBlk()
  1933. {
  1934. TraceParamBlock     param;
  1935. register    short    result;
  1936.  
  1937.     param.u.getStatus.statusPtr = &fTraceStatus;
  1938.     result = Control(fTraceRefNum, kGetTraceStatus, (Ptr) ¶m);
  1939.     fTraceOnStartup    = fTraceStatus.TraceOnStartup;
  1940.     return result;
  1941. }
  1942.  
  1943. // Writes the contents of the buffer to disk.
  1944. void     t_cdev::WriteBufferNow()
  1945. {
  1946. TraceParamBlock     param;
  1947. register    short    result;
  1948.     result = Control(fTraceRefNum, kWriteTraceBuffer, (Ptr) ¶m);
  1949.     ReportResult(result,kFileError);
  1950.     
  1951. }
  1952.